<template>
	<view>
		<xiaolu-tree @itemClick="handleClick" :checkList="checkList" v-if="tree.length>0" :props="prop" @sendValue="confirm" :isCheck="true" :trees="tree" />
		<view class="btn box_sizing">
			<button class="sureBtn" type="primary" @click="backConfirm">确认</button>
		</view>
	</view>
</template>

<script>
	import XiaoluTree from '@/components/xiaolu-tree/tree.vue';
	// import dataList from './data.js'
	// import dataList from './test.js'
	export default {
		components: {
			XiaoluTree
		},
		data() {
			return {
				unitTree: {},
				list: [],
				params: {
					单位id: null
				},
				tree: [],
				checkList: [],
				prop:{},
				aprop: {
					label: 'name',
					children: 'children',
					multiple:true
				},
				bprop: {
					label: 'name',
					children: 'children',
					multiple:true,
					checkStrictly:true
				},
				cprop: {
					label: 'name',
					children: 'children',
					multiple:false,
					nodes:false
				},
				dprop: {
					label: 'name',
					children: 'children',
					multiple:false,
					nodes:true
				}
			}
		},
		onLoad(o) {
			if(o.type==0){
				this.prop=this.aprop;
				this.checkList = JSON.parse(o.item)
				this.checks()
			}else if(o.type==1){
				this.prop=this.bprop;
				this.checkList = JSON.parse(o.item)
				this.checks()
			}else if(o.type==2){
				this.prop=this.cprop;
			}else{
				this.prop=this.dprop;
			}
			this.getUnitTree()
			// setTimeout(() => {
			// 	this.tree = JSON.parse(JSON.stringify(this.list))
			// }, 3000)
			// this.getWorkerList()
		},
		onReady() {
			// this.tree = this.list
			// uni.showLoading({
			// 	title:"加载中"
			// })
			// setTimeout(()=>{
			// 	this.tree=dataList;
			// 	uni.hideLoading()
			// },400)
		},
		methods: {
			findNode(tree, id) {
				let that = this
			
				function findNode(item, id) {
					let result = null
					if (item.id == id) {
						result = item
					} else {
						if (item.children && item.children.length > 0) {
							for (let i = 0; i < item.children.length; i++) {
								let t = findNode(item.children[i], id)
								if (t) {
									result = t;
									break;
								}
							}
						}
					}
					return result
				}
				return findNode(tree, id)
			},
			initData() {
				let unitList = uni.getStorageSync('userInfo').单位
				unitList.forEach(item => {
					this.findTreeNode(item.单位id)
				})
				console.log('所有单位', this.list)
				
			},
			findTreeNode(id) {
				let currentTree = this.findNode(this.unitTree, id)
				console.log('树节点', currentTree)
				currentTree.checked = false
				if(currentTree.type) {
					currentTree.user = false
				}else {
					currentTree.user = true
				}
				// currentTree.user = false
				currentTree.name = currentTree.title
				currentTree.children = currentTree.children || []
				
				this.list.push(currentTree)
			},
			getUnitTree() {
				uni.showLoading({
					mask: true
				})
				this.$http.post('worksys/unit/tree').then(res => {
					if(res.code === 10000) {
						// console.log(res)
						this.unitTree = res.data
						this.initData()
						this.tree = JSON.parse(JSON.stringify(this.list))
						// console.log(this.tree)
					}
				})
			},
			getWorkerList(item) {
				// this.params.单位id = uni.getStorageSync('userInfo').单位[0].单位id
				this.$http.post('worksys/worker/list', {
					单位id: item.id,
					功能模块: ['任务管理']
					}).then(res => {
					if(res.code === 10000) {
						console.log(res)
						res.data.list = res.data.list || []
						res.data.list.forEach(item => {
							item.checked = false
							item.user = true
							item.name = item.人员名称
							item.children = []
							item.pid = ''
						})
						// let current = this.list.find(item => item.id === id)
						item.children = [...item.children, ...res.data.list]
					}
				})
			},
			//检查默认选中,要是字段钟本身包含了checked字段就不需要这个
			checks() {
				var that = this;
				for (var i = 0, len = that.tree.length; i < len; i++) {
					for (var j = 0, lens = that.checkList.length; j < lens; j++) {
						if (that.checkList[j].id === that.tree[i].id) {
							that.$set(that.tree[i], 'checked', true)
							break
						} else {
							that.$set(that.tree[i], 'checked', false)
						}
					}
				}
			},
			//获取选中的
			confirm(val) {
				console.log(val)
				this.checkList = val
			},
			backConfirm() {
				console.log(this.type)
				console.log(this.checkList)
				var pages = getCurrentPages();
				var currPage = pages[pages.length - 1]; //当前页面
				var prevPage = pages[pages.length - 2]; //上一个页面
				//prevPage.query = this.checkList//小程序写法
				// prevPage.$vm.query = this.checkList//h5写法
				prevPage.$vm.query = this.checkList
				prevPage.$vm.params.转派人员 = this.checkList[0].name
				prevPage.$vm.params.转派人员id = this.checkList[0].id
				// uni.setStorageSync('query', this.checkList)
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss">
	.box_sizing {
		-webkit-box-sizing: border-box;
		-moz-box-sizing: border-box;
		box-sizing: border-box;
	}

	.btn {
		position: fixed;
		bottom: 0;
		padding: 10px;
		background-color: #fff;
		width: 100%;

		.sureBtn {
			background-color: #1152FC;
			color: #fff;
		}
	}
</style>
